package service

import (
	"errors"
	"gin_demo/dao"
)

type UserService struct {
}

func (s *UserService) QueryAllUser() (users []*dao.User, err error) {
	users = dao.UserModel.QueryAllUser()
	if users == nil {
		return nil, errors.New("没有数据")
	}
	return
}
func (s *UserService) GetUserByUsername(argUser *dao.User) (user *dao.User, err error) {
	if argUser.Username == "" {
		return nil, errors.New("用户名不能为空")
	}

	user = dao.UserModel.GetUser(argUser)
	if argUser == nil {
		return nil, errors.New("用户名错误")
	}
	return
}

func (s *UserService) GetUserByUserNameAndPassword(argUser *dao.User) (user *dao.User, err error) {
	if argUser.Password == "" || argUser.Username == "" {
		return nil, errors.New("用户或密码为空")
	}

	user = dao.UserModel.GetUser(argUser)
	if user == nil {
		return nil, errors.New("用户或密码错误")
	}
	return
}
func (s *UserService) RegUser(user *dao.User) bool {
	return dao.UserModel.InsertUser(user)
}
func (s *UserService) DelUserByUsername(user *dao.User) bool {
	if user.Username == "" {
		return false
	}
	return dao.UserModel.DelUser(user)
}
func (s *UserService) UpdatePasswordByUsername(user *dao.User) bool {
	if user.Username == "" {
		return false
	}
	return dao.UserModel.UpdateUser(user)
}
